From 4b64c0d7ea1fd15dee69be0754bf8a8d07838946 Mon Sep 17 00:00:00 2001 From: "mjw@wray-m-3.hpl.hp.com" Date: Mon, 22 Nov 2004 16:41:50 +0000 Subject: [PATCH] bitkeeper revision 1.1159.183.2 (41a216ce6YIv6IWbateVWedQylK5rA) Restructure libxutil to prepare for adding vnets. Move common files from xfrd into libxutil. --- .rootkeys | 25 +-- tools/libxutil/Makefile | 13 +- tools/libxutil/debug.h | 72 ++++++++ tools/{xfrd => libxutil}/enum.c | 0 tools/{xfrd => libxutil}/enum.h | 0 tools/{xfrd => libxutil}/hash_table.c | 0 tools/{xfrd => libxutil}/hash_table.h | 0 tools/libxutil/iostream.c | 18 ++ tools/libxutil/iostream.h | 30 +++- tools/libxutil/kernel_stream.c | 8 +- tools/{xfrd => libxutil}/lexis.c | 1 + tools/{xfrd => libxutil}/lexis.h | 1 + tools/libxutil/socket_stream.c | 230 +++++++++++++++++++++++++ tools/libxutil/socket_stream.h | 53 ++++++ tools/libxutil/string_stream.c | 2 +- tools/libxutil/string_stream.h | 2 +- tools/{xfrd => libxutil}/sxpr.c | 6 + tools/{xfrd => libxutil}/sxpr.h | 6 + tools/{xfrd => libxutil}/sxpr_parser.c | 16 ++ tools/{xfrd => libxutil}/sxpr_parser.h | 1 + tools/libxutil/sys_net.c | 4 - tools/libxutil/util.c | 106 ++++++++++++ tools/libxutil/util.h | 28 +++ tools/xfrd/Make.xfrd | 1 - 24 files changed, 596 insertions(+), 27 deletions(-) create mode 100644 tools/libxutil/debug.h rename tools/{xfrd => libxutil}/enum.c (100%) rename tools/{xfrd => libxutil}/enum.h (100%) rename tools/{xfrd => libxutil}/hash_table.c (100%) rename tools/{xfrd => libxutil}/hash_table.h (100%) rename tools/{xfrd => libxutil}/lexis.c (97%) rename tools/{xfrd => libxutil}/lexis.h (98%) create mode 100644 tools/libxutil/socket_stream.c create mode 100644 tools/libxutil/socket_stream.h rename tools/{xfrd => libxutil}/sxpr.c (99%) rename tools/{xfrd => libxutil}/sxpr.h (98%) rename tools/{xfrd => libxutil}/sxpr_parser.c (96%) rename tools/{xfrd => libxutil}/sxpr_parser.h (98%) create mode 100644 tools/libxutil/util.c create mode 100644 tools/libxutil/util.h diff --git a/.rootkeys b/.rootkeys index 8f915eb939..f12d2ae237 100644 --- a/.rootkeys +++ b/.rootkeys @@ -336,20 +336,35 @@ 40e1b09dMYB4ItGCqcMIzirdMd9I-w tools/libxutil/Makefile 40e033325Sjqs-_4TuzeUEprP_gYFg tools/libxutil/allocate.c 40e03332KYz7o1bn2MG_KPbBlyoIMA tools/libxutil/allocate.h +41a216cav5JJbtDQnusfuMa_1x_Xpw tools/libxutil/debug.h +40e9808eyjiahG5uF6AMelNVujBzCg tools/libxutil/enum.c +40e9808eZpbdn9q2KSSMGCNvY_ZgpQ tools/libxutil/enum.h 40e03332p5Dc_owJQRuN72ymJZddFQ tools/libxutil/file_stream.c 40e03332jWfB2viAhLSkq1WK0r_iDQ tools/libxutil/file_stream.h 40e03332rUjNMGg11n2rN6V4DCrvOg tools/libxutil/gzip_stream.c 40e033321O5Qg22haLoq5lpmk4tooQ tools/libxutil/gzip_stream.h +40e9808easXCzzAZQodEfKAhgUXSPA tools/libxutil/hash_table.c +40e9808e94BNXIVVKBFHC3rnkvwtJg tools/libxutil/hash_table.h 40e03332ihnBGzHykVwZnFmkAppb4g tools/libxutil/iostream.c 40e03332UGwbLR4wsw4ft14p0Yw5pg tools/libxutil/iostream.h 40e0333245DLDzJemeSVBLuutHtzEQ tools/libxutil/kernel_stream.c 40e03332aK0GkgpDdc-PVTkWKTeOBg tools/libxutil/kernel_stream.h +40e9808epW9iHcLXuO3QfUfLzB7onw tools/libxutil/lexis.c +40e9808egccMhCizayQRGtpBA3L5MQ tools/libxutil/lexis.h +41a216caM4z39Fzjb91rv9Ed_4By1A tools/libxutil/socket_stream.c +41a216caqinvF1I5FQMHA4HTRz8MSA tools/libxutil/socket_stream.h 40e03332KT_tnnoAMbPVAZBB7kSOAQ tools/libxutil/string_stream.c 40e03332-VtK6_OZa1vMHXFil8uq6w tools/libxutil/string_stream.h +40e9808e5_PLdodqVOSx0b4T_f5aeg tools/libxutil/sxpr.c +40e9808e0O4sHZtkDv5hlSqjYcdQAQ tools/libxutil/sxpr.h +40ec1cc6SIiGbynOi-1NtPesOlzF-Q tools/libxutil/sxpr_parser.c +40ec1cc6wpvvGxZiq4EFvNOcw0tUFg tools/libxutil/sxpr_parser.h 40e03332Rkvq6nn_UNjzAAK_Tk9v1g tools/libxutil/sys_net.c 40e03332lQHvQHw4Rh7VsT1_sui29A tools/libxutil/sys_net.h 40e033321smklZd7bDSdWvQCeIshtg tools/libxutil/sys_string.c 40e03332h5V611rRWURRLqb1Ekatxg tools/libxutil/sys_string.h +41a216cayFe2FQroFuzvNPw1AvNiqQ tools/libxutil/util.c +41a216ca7mgVSnCBHPCLkGOIqPS1CQ tools/libxutil/util.h 3f776bd2Xd-dUcPKlPN2vG89VGtfvQ tools/misc/Makefile 40ab2cfawIw8tsYo0dQKtp83h4qfTQ tools/misc/fakei386xen 3f6dc136ZKOjd8PIqLbFBl_v-rnkGg tools/misc/miniterm/Makefile @@ -544,23 +559,13 @@ 40e9808eysqT4VNDlJFqsZB2rdg4Qw tools/xfrd/connection.c 40e9808eyXfJUi4E0C3WSgrEXqQ1sQ tools/xfrd/connection.h 40e9808eULGwffNOE4kBrAfZ9YAVMA tools/xfrd/debug.h -40e9808eyjiahG5uF6AMelNVujBzCg tools/xfrd/enum.c -40e9808eZpbdn9q2KSSMGCNvY_ZgpQ tools/xfrd/enum.h -40e9808easXCzzAZQodEfKAhgUXSPA tools/xfrd/hash_table.c -40e9808e94BNXIVVKBFHC3rnkvwtJg tools/xfrd/hash_table.h 411b5139tfKZfWs1LQHmwDR_wjKoxQ tools/xfrd/http.h -40e9808epW9iHcLXuO3QfUfLzB7onw tools/xfrd/lexis.c -40e9808egccMhCizayQRGtpBA3L5MQ tools/xfrd/lexis.h 40e9808ePADCSKL1YgGCt2TbYPnYkw tools/xfrd/lzi_stream.c 40e9808eDNAdpF71o5teYb9DTT-PRw tools/xfrd/lzi_stream.h 40e9808eQxi0EzTcPJtosrzxEIjA-Q tools/xfrd/marshal.c 40e9808etg13xfRm0Lqd8vY-jHOoTg tools/xfrd/marshal.h 40e9808eCsmywryb036TdtRMJHDMmQ tools/xfrd/select.c 40e9808e99OcM547cKMTfmCVSoWVAw tools/xfrd/select.h -40e9808e5_PLdodqVOSx0b4T_f5aeg tools/xfrd/sxpr.c -40e9808e0O4sHZtkDv5hlSqjYcdQAQ tools/xfrd/sxpr.h -40ec1cc6SIiGbynOi-1NtPesOlzF-Q tools/xfrd/sxpr_parser.c -40ec1cc6wpvvGxZiq4EFvNOcw0tUFg tools/xfrd/sxpr_parser.h 40e9808eF3NVldqRNS5IHM8gbFAvpw tools/xfrd/xdr.c 40e9808ezXzoRHm7pybXU69NtnjimA tools/xfrd/xdr.h 40e9808edpUtf4bJ8IbqClPJj_OvbA tools/xfrd/xen_domain.c diff --git a/tools/libxutil/Makefile b/tools/libxutil/Makefile index 6986c96e79..85acd2063c 100644 --- a/tools/libxutil/Makefile +++ b/tools/libxutil/Makefile @@ -5,11 +5,18 @@ CC = gcc LIB_SRCS := LIB_SRCS += allocate.c +LIB_SRCS += enum.c LIB_SRCS += file_stream.c LIB_SRCS += gzip_stream.c +LIB_SRCS += hash_table.c LIB_SRCS += iostream.c -#LIB_SRCS += sys_net.c +LIB_SRCS += lexis.c +LIB_SRCS += string_stream.c +LIB_SRCS += sxpr.c +LIB_SRCS += sxpr_parser.c +LIB_SRCS += sys_net.c LIB_SRCS += sys_string.c +LIB_SRCS += util.c LIB_OBJS := $(LIB_SRCS:.c=.o) @@ -29,6 +36,7 @@ LIB_NAME := libxutil LIB := $(LIB_NAME).so LIB += $(LIB_NAME).so.$(MAJOR) LIB += $(LIB_NAME).so.$(MAJOR).$(MINOR) +LIB += $(LIB_NAME).a all: check-for-zlib $(MAKE) $(LIB) @@ -42,6 +50,9 @@ $(LIB_NAME).so.$(MAJOR): $(LIB_NAME).so.$(MAJOR).$(MINOR) $(LIB_NAME).so.$(MAJOR).$(MINOR): $(LIB_OBJS) $(CC) -Wl,-soname -Wl,$(LIB_NAME).so.$(MAJOR) -shared -o $@ $^ +$(LIB_NAME).a: $(LIB_OBJS) + $(AR) rc $@ $^ + check-for-zlib: @if [ ! -e /usr/include/zlib.h ]; then \ echo "***********************************************************"; \ diff --git a/tools/libxutil/debug.h b/tools/libxutil/debug.h new file mode 100644 index 0000000000..1f5a19d54d --- /dev/null +++ b/tools/libxutil/debug.h @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2004 Mike Wray + * + * This library is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#ifndef _XUTIL_DEBUG_H_ +#define _XUTIL_DEBUG_H_ + +#ifndef MODULE_NAME +#define MODULE_NAME "" +#endif + +#ifdef __KERNEL__ +#include +#include + +#ifdef DEBUG + +#define dprintf(fmt, args...) printk(KERN_DEBUG "[DBG] " MODULE_NAME ">%s" fmt, __FUNCTION__, ##args) +#define wprintf(fmt, args...) printk(KERN_WARNING "[WRN] " MODULE_NAME ">%s" fmt, __FUNCTION__, ##args) +#define iprintf(fmt, args...) printk(KERN_INFO "[INF] " MODULE_NAME ">%s" fmt, __FUNCTION__, ##args) +#define eprintf(fmt, args...) printk(KERN_ERR "[ERR] " MODULE_NAME ">%s" fmt, __FUNCTION__, ##args) + +#else + +#define dprintf(fmt, args...) do {} while(0) +#define wprintf(fmt, args...) printk(KERN_WARNING "[WRN] " MODULE_NAME fmt, ##args) +#define iprintf(fmt, args...) printk(KERN_INFO "[INF] " MODULE_NAME fmt, ##args) +#define eprintf(fmt, args...) printk(KERN_ERR "[ERR] " MODULE_NAME fmt, ##args) + +#endif + +#else + +#include + +#ifdef DEBUG + +#define dprintf(fmt, args...) fprintf(stdout, "%d [DBG] " MODULE_NAME ">%s" fmt, getpid(), __FUNCTION__, ##args) +#define wprintf(fmt, args...) fprintf(stderr, "%d [WRN] " MODULE_NAME ">%s" fmt, getpid(),__FUNCTION__, ##args) +#define iprintf(fmt, args...) fprintf(stderr, "%d [INF] " MODULE_NAME ">%s" fmt, getpid(),__FUNCTION__, ##args) +#define eprintf(fmt, args...) fprintf(stderr, "%d [ERR] " MODULE_NAME ">%s" fmt, getpid(),__FUNCTION__, ##args) + +#else + +#define dprintf(fmt, args...) do {} while(0) +#define wprintf(fmt, args...) fprintf(stderr, "%d [WRN] " MODULE_NAME fmt, getpid(), ##args) +#define iprintf(fmt, args...) fprintf(stderr, "%d [INF] " MODULE_NAME fmt, getpid(), ##args) +#define eprintf(fmt, args...) fprintf(stderr, "%d [ERR] " MODULE_NAME fmt, getpid(), ##args) + +#endif + +#endif + +/** Print format for an IP address. + * See NIPQUAD(), HIPQUAD() + */ +#define IPFMT "%u.%u.%u.%u" + +#endif /* ! _XUTIL_DEBUG_H_ */ diff --git a/tools/xfrd/enum.c b/tools/libxutil/enum.c similarity index 100% rename from tools/xfrd/enum.c rename to tools/libxutil/enum.c diff --git a/tools/xfrd/enum.h b/tools/libxutil/enum.h similarity index 100% rename from tools/xfrd/enum.h rename to tools/libxutil/enum.h diff --git a/tools/xfrd/hash_table.c b/tools/libxutil/hash_table.c similarity index 100% rename from tools/xfrd/hash_table.c rename to tools/libxutil/hash_table.c diff --git a/tools/xfrd/hash_table.h b/tools/libxutil/hash_table.h similarity index 100% rename from tools/xfrd/hash_table.h rename to tools/libxutil/hash_table.h diff --git a/tools/libxutil/iostream.c b/tools/libxutil/iostream.c index e9980838f7..39a62173ad 100644 --- a/tools/libxutil/iostream.c +++ b/tools/libxutil/iostream.c @@ -1,3 +1,21 @@ +/* + * Copyright (C) 2001 - 2004 Mike Wray + * + * This library is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + #include "iostream.h" #include "sys_string.h" diff --git a/tools/libxutil/iostream.h b/tools/libxutil/iostream.h index 5dbe14a0b4..4786f325b2 100644 --- a/tools/libxutil/iostream.h +++ b/tools/libxutil/iostream.h @@ -1,14 +1,34 @@ -#ifndef _XC_LINUX_SAVE_H_ -#define _XC_LINUX_SAVE_H_ +/* + * Copyright (C) 2001 - 2004 Mike Wray + * + * This library is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _XUTIL_IOSTREAM_H_ +#define _XUTIL_IOSTREAM_H_ #include -#include -#include #ifdef __KERNEL__ +#include +#include #include #else #include +#include +#include #endif #include "allocate.h" @@ -240,4 +260,4 @@ static inline int IOStream_get_written(IOStream *stream){ } -#endif /* ! _XC_LINUX_SAVE_H_ */ +#endif /* ! _XUTIL_IOSTREAM_H_ */ diff --git a/tools/libxutil/kernel_stream.c b/tools/libxutil/kernel_stream.c index 345b048015..a13051eec8 100644 --- a/tools/libxutil/kernel_stream.c +++ b/tools/libxutil/kernel_stream.c @@ -50,7 +50,7 @@ typedef struct KernelData { char buf[BUF_N]; } KernelData; -static int kernel_write(IOStream *s, const char *msg, int n); +static int kernel_write(IOStream *s, const void *msg, size_t n); static void kernel_free(IOStream *s); static void kernel_stream_lock(IOStream *s); static void kernel_stream_unlock(IOStream *s); @@ -145,13 +145,13 @@ void kernel_stream_unlock(IOStream *s){ * @param args print arguments * @return result of the print */ -static int kernel_write(IOStream *stream, const char *buf, int n){ +static int kernel_write(IOStream *stream, const void *buf, size_t n){ KernelData *kdata = get_kernel_data(stream); int k; k = kdata->buf_n - 1; if(n < k) k = n; memcpy(kdata->buf, buf, k); - kdata->buf[k] = '\0' + kdata->buf[k] = '\0'; printk(kdata->buf); return k; } @@ -167,7 +167,7 @@ static void kernel_free(IOStream *io){ KernelData *kdata; if(io == &iokernel) return; kdata = get_kernel_data(io); - zero(kdata, sizeof(*kdata)); + memset(kdata, 0, sizeof(*kdata)); deallocate(kdata); } #endif /* __KERNEL__ */ diff --git a/tools/xfrd/lexis.c b/tools/libxutil/lexis.c similarity index 97% rename from tools/xfrd/lexis.c rename to tools/libxutil/lexis.c index 26d2ec4d5b..d3441f0c38 100644 --- a/tools/xfrd/lexis.c +++ b/tools/libxutil/lexis.c @@ -1,4 +1,5 @@ /* + * Copyright (C) 2001 - 2004 Mike Wray * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as diff --git a/tools/xfrd/lexis.h b/tools/libxutil/lexis.h similarity index 98% rename from tools/xfrd/lexis.h rename to tools/libxutil/lexis.h index 796a8a415a..be8fb653d3 100644 --- a/tools/xfrd/lexis.h +++ b/tools/libxutil/lexis.h @@ -1,4 +1,5 @@ /* + * Copyright (C) 2001 - 2004 Mike Wray * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as diff --git a/tools/libxutil/socket_stream.c b/tools/libxutil/socket_stream.c new file mode 100644 index 0000000000..9e90b4e06e --- /dev/null +++ b/tools/libxutil/socket_stream.c @@ -0,0 +1,230 @@ +/* + * Copyright (C) 2004 Mike Wray + * + * This library is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/** @file + * An IOStream implementation using sockets. + */ +#ifndef __KERNEL__ + +#include +#include +#include +#include +#include +#include "allocate.h" +#include "socket_stream.h" + +#define MODULE_NAME "sock" +#define DEBUG 0 +//#undef DEBUG +#include "debug.h" + +static int socket_read(IOStream *s, void *buf, size_t n); +static int socket_write(IOStream *s, const void *buf, size_t n); +static int socket_error(IOStream *s); +static int socket_close(IOStream *s); +static void socket_free(IOStream *s); +static int socket_flush(IOStream *s); + +/** Methods used by a socket IOStream. */ +static const IOMethods socket_methods = { + read: socket_read, + write: socket_write, + error: socket_error, + close: socket_close, + free: socket_free, + flush: socket_flush, +}; + +/** Get the socket data. + * + * @param io socket stream + * @return data + */ +static inline SocketData * socket_data(IOStream *io){ + return (SocketData *)io->data; +} + +/** Test if a stream is a socket stream. + * + * @param io stream + * @return 0 if a socket stream, -EINVAL if not + */ +int socket_stream_check(IOStream *io){ + return (io && io->methods == &socket_methods ? 0 : -EINVAL); +} + +/** Get the data for a socket stream. + * + * @param io stream + * @param data return value for the data + * @return 0 if a socket stream, -EINVAL if not + */ +int socket_stream_data(IOStream *io, SocketData **data){ + int err = socket_stream_check(io); + if(err){ + *data = NULL; + } else { + *data = socket_data(io); + } + return err; +} + +/** Set the destination address for a socket stream. + * + * @param io stream + * @param addr address + * @return 0 if a socket stream, -EINVAL if not + */ +int socket_stream_set_addr(IOStream *io, struct sockaddr_in *addr){ + int err = 0; + SocketData *data = NULL; + err = socket_stream_data(io, &data); + if(!err){ + data->daddr = *addr; + } + return err; +} + +/** Set the send flags for a socket stream. + * + * @param io stream + * @param flags flags + * @return 0 if a socket stream, -EINVAL if not + */ +int socket_stream_set_flags(IOStream *io, int flags){ + int err = 0; + SocketData *data = NULL; + err = socket_stream_data(io, &data); + if(!err){ + data->flags = flags; + } + return err; +} + +/** Write to the underlying socket using sendto. + * + * @param stream input + * @param buf where to put input + * @param n number of bytes to write + * @return number of bytes written + */ +static int socket_write(IOStream *s, const void *buf, size_t n){ + SocketData *data = socket_data(s); + struct sockaddr *daddr = (struct sockaddr *)&data->daddr; + socklen_t daddr_n = sizeof(data->daddr); + int k; + dprintf("> sock=%d addr=%s:%d n=%d\n", + data->fd, inet_ntoa(data->daddr.sin_addr), ntohs(data->daddr.sin_port), n); + if(0){ + struct sockaddr_in self = {}; + socklen_t self_n; + getsockname(data->fd, (struct sockaddr *)&self, &self_n); + dprintf("> sockname sock=%d %s:%d\n", + data->fd, inet_ntoa(self.sin_addr), ntohs(self.sin_port)); + } + k = sendto(data->fd, buf, n, data->flags, daddr, daddr_n); + dprintf("> sendto=%d\n", k); + return k; +} + +/** Read from the underlying stream using recv(); + * + * @param stream input + * @param buf where to put input + * @param n number of bytes to read + * @return number of bytes read + */ +static int socket_read(IOStream *s, void *buf, size_t n){ + SocketData *data = socket_data(s); + int k; + struct sockaddr *saddr = (struct sockaddr *)&data->saddr; + socklen_t saddr_n = sizeof(data->saddr); + k = recvfrom(data->fd, buf, n, data->flags, saddr, &saddr_n); + return k; +} + +/** Flush the socket (no-op). + * + * @param s socket stream + * @return 0 on success, error code otherwise + */ +static int socket_flush(IOStream *s){ + return 0; +} + +/** Check if a socket stream has an error (no-op). + * + * @param s socket stream + * @return 1 if has an error, 0 otherwise + */ +static int socket_error(IOStream *s){ + // Read SOL_SOCKET/SO_ERROR ? + return 0; +} + +/** Close a socket stream. + * + * @param s socket stream to close + * @return result of the close + */ +static int socket_close(IOStream *s){ + SocketData *data = socket_data(s); + return close(data->fd); +} + +/** Free a socket stream. + * + * @param s socket stream + */ +static void socket_free(IOStream *s){ + SocketData *data = socket_data(s); + deallocate(data); +} + +/** Create an IOStream for a socket. + * + * @param fd socket to wtap + * @return new IOStream using fd for i/o + */ +IOStream *socket_stream_new(int fd){ + int err = -ENOMEM; + IOStream *io = NULL; + SocketData *data = NULL; + + io = ALLOCATE(IOStream); + if(!io) goto exit; + io->methods = &socket_methods; + data = ALLOCATE(SocketData); + if(!data) goto exit; + io->data = data; + data->fd = fd; + data->buf_n = sizeof(data->buf); + err = 0; + exit: + if(err){ + if(io){ + if(data) deallocate(data); + deallocate(io); + io = NULL; + } + } + return io; +} + +#endif diff --git a/tools/libxutil/socket_stream.h b/tools/libxutil/socket_stream.h new file mode 100644 index 0000000000..9da23e9f01 --- /dev/null +++ b/tools/libxutil/socket_stream.h @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2004 Mike Wray + * + * This library is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _XEN_LIB_SOCKET_STREAM_H_ +#define _XEN_LIB_SOCKET_STREAM_H_ + +#ifndef __KERNEL__ +#include "iostream.h" +#include + +#include +#include +#include + +/** Data associated with a socket stream. */ +typedef struct SocketData { + /** The socket file descriptor. */ + int fd; + /** Source address from last read (recvfrom). */ + struct sockaddr_in saddr; + /** Destination address for writes (sendto). */ + struct sockaddr_in daddr; + /** Write flags (sendto). */ + int flags; + /** Buffer size. */ + int buf_n; + /** Buffer for formatted printing. */ + char buf[1024]; +} SocketData; + +extern IOStream *socket_stream_new(int fd); +extern int socket_stream_data(IOStream *io, SocketData **data); +extern int socket_stream_check(IOStream *io); +extern int socket_stream_set_addr(IOStream *io, struct sockaddr_in *addr); +extern int socket_stream_set_flags(IOStream *io, int flags); + +#endif +#endif /* !_XEN_LIB_SOCKET_STREAM_H_ */ diff --git a/tools/libxutil/string_stream.c b/tools/libxutil/string_stream.c index 495238d74a..a535c922ff 100644 --- a/tools/libxutil/string_stream.c +++ b/tools/libxutil/string_stream.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2001, 2002 Hewlett-Packard Company. + * Copyright (C) 2001 - 2004 Mike Wray * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by diff --git a/tools/libxutil/string_stream.h b/tools/libxutil/string_stream.h index b6cbc0f3df..246e63d2fe 100644 --- a/tools/libxutil/string_stream.h +++ b/tools/libxutil/string_stream.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2001, 2002 Hewlett-Packard Company. + * Copyright (C) 2001 - 2004 Mike Wray * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by diff --git a/tools/xfrd/sxpr.c b/tools/libxutil/sxpr.c similarity index 99% rename from tools/xfrd/sxpr.c rename to tools/libxutil/sxpr.c index 81fd14801c..5a9364a317 100644 --- a/tools/xfrd/sxpr.c +++ b/tools/libxutil/sxpr.c @@ -1,4 +1,5 @@ /* + * Copyright (C) 2001 - 2004 Mike Wray * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as @@ -21,7 +22,12 @@ #include "hash_table.h" #include "sxpr.h" +#ifdef __KERNEL__ +#include +#else #include +#endif + #undef free /** @file diff --git a/tools/xfrd/sxpr.h b/tools/libxutil/sxpr.h similarity index 98% rename from tools/xfrd/sxpr.h rename to tools/libxutil/sxpr.h index a5875c76bd..8b01e84736 100644 --- a/tools/xfrd/sxpr.h +++ b/tools/libxutil/sxpr.h @@ -1,4 +1,5 @@ /* + * Copyright (C) 2001 - 2004 Mike Wray * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as @@ -16,7 +17,12 @@ #ifndef _XUTIL_SXPR_H_ #define _XUTIL_SXPR_H_ +#ifdef __KERNEL__ +#include +#include +#else #include +#endif #include "hash_table.h" #include "iostream.h" diff --git a/tools/xfrd/sxpr_parser.c b/tools/libxutil/sxpr_parser.c similarity index 96% rename from tools/xfrd/sxpr_parser.c rename to tools/libxutil/sxpr_parser.c index dbe386a862..96b4d08516 100644 --- a/tools/xfrd/sxpr_parser.c +++ b/tools/libxutil/sxpr_parser.c @@ -1,3 +1,19 @@ +/* + * Copyright (C) 2001 - 2004 Mike Wray + * + * This library is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of the + * License, or (at your option) any later version. This library is + * distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ #ifdef __KERNEL__ # include diff --git a/tools/xfrd/sxpr_parser.h b/tools/libxutil/sxpr_parser.h similarity index 98% rename from tools/xfrd/sxpr_parser.h rename to tools/libxutil/sxpr_parser.h index 64a417608c..0736fd3d40 100644 --- a/tools/xfrd/sxpr_parser.h +++ b/tools/libxutil/sxpr_parser.h @@ -1,4 +1,5 @@ /* + * Copyright (C) 2001 - 2004 Mike Wray * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as diff --git a/tools/libxutil/sys_net.c b/tools/libxutil/sys_net.c index f38d919d9c..94da58d63e 100644 --- a/tools/libxutil/sys_net.c +++ b/tools/libxutil/sys_net.c @@ -232,21 +232,17 @@ char *get_port_service(unsigned long port){ int convert_service_to_port(const char *s, unsigned long *port){ int err = 0; unsigned long value; - printf("%s> %s\n", __FUNCTION__, s); if(convert_atoul(s, &value) == 0){ int ok = (0 <= value) && (value <= PORT_MAX); - printf("> value = %ld\n", value); if(ok){ value = htons((unsigned short)value); } else { err = -EINVAL; } } else { - printf("> get_service_port...\n"); err = get_service_port(s, &value); } *port = (err ? 0: value); - printf("%s< err=%d\n", __FUNCTION__, err); return err; } diff --git a/tools/libxutil/util.c b/tools/libxutil/util.c new file mode 100644 index 0000000000..0ac388b3b8 --- /dev/null +++ b/tools/libxutil/util.c @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2002 - 2004 Mike Wray . + * + * This library is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of the + * License, or (at your option) any later version. This library is + * distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "sys_net.h" +#include "sys_string.h" + +#ifndef __KERNEL__ +# include +# include +#endif + +#include "util.h" + + +/** @file Various utility functions. + */ + +/** Print an address (in network order) as an IPv4 address string + * in dot notation. + * + * @param io where to print address + * @param address to print (in network order) + * @return bytes printed + */ +int print_address(IOStream *io, unsigned long address){ +#ifdef __KERNEL__ + address = ntohl(address); + return IOStream_print(io, "%u.%u.%u.%u", + (unsigned)((address >> 24) & 0xff), + (unsigned)((address >> 16) & 0xff), + (unsigned)((address >> 8) & 0xff), + (unsigned)((address ) & 0xff)); +#else + struct in_addr inaddr = { s_addr: address }; + return IOStream_print(io, inet_ntoa(inaddr)); +#endif +} + +/** Get the protocol number for a protocol. + * + * @param name protocol name + * @param protocol where to put the protocol number + * @return 0 if OK, error otherwise + */ +int get_protocol_number(char *name, unsigned long *protocol){ +#ifdef __KERNEL__ + return -1; +#else + struct protoent *proto = getprotobyname(name); + if(!proto){ + return -1; + } + *protocol = proto->p_proto; + return 0; +#endif +} + +/** Get the protocol name for a protocol number. + * + * @param protocol number + * @return name or null + */ +char *get_protocol_name(unsigned long protocol){ +#ifdef __KERNEL__ + return 0; +#else + struct protoent *proto = getprotobynumber(protocol); + if(!proto){ + return 0; + } + return proto->p_name; +#endif +} + +/** Get the host name for an address. + * + * @param addr address + * @return host name or null + */ +char *get_host_name(unsigned long addr){ +#ifdef __KERNEL__ + return 0; +#else + struct in_addr inaddr; + struct hostent *host = 0; + + inaddr.s_addr = addr; + host = gethostbyaddr((char*)&inaddr, sizeof(inaddr), AF_INET); + if(!host) return NULL; + return host->h_name; +#endif +} diff --git a/tools/libxutil/util.h b/tools/libxutil/util.h new file mode 100644 index 0000000000..b4a170512f --- /dev/null +++ b/tools/libxutil/util.h @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2002 - 2004 Mike Wray . + * + * This library is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of the + * License, or (at your option) any later version. This library is + * distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _XEN_LIB_UTIL_H_ +#define _XEN_LIB_UTIL_H_ + +#include "iostream.h" + +extern int print_address(IOStream *io, unsigned long address); +extern int get_protocol_number(char *name, unsigned long *protocol); +extern char *get_protocol_name(unsigned long protocol); +extern char *get_host_name(unsigned long addr); + +#endif /* ! _XEN_LIB_UTIL_H_ */ diff --git a/tools/xfrd/Make.xfrd b/tools/xfrd/Make.xfrd index bb746d6bef..3bbfe4d5d6 100644 --- a/tools/xfrd/Make.xfrd +++ b/tools/xfrd/Make.xfrd @@ -26,7 +26,6 @@ UTIL_LIB_SRC += xdr.c XFRD_PROG_SRC = XFRD_PROG_SRC += xfrd.c -#XFRD_PROG_SRC += xfr_msg.c XFRD_PROG_SRC += xen_domain.c XFRD_PROG_SRC += select.c XFRD_PROG_SRC += connection.c -- 2.30.2